package com.wj.service.impl;

import com.wj.domain.entity.BookShelfEntity;
import com.wj.domain.vo.BookShelfVO;
import com.wj.infrastructure.mapper.BookMapper;
import com.wj.infrastructure.mapper.BookShelfMapper;
import com.wj.infrastructure.repository.BookRepository;
import com.wj.infrastructure.repository.BookShelfRepository;
import com.wj.service.BookShelfService;
import com.wj.tool.common.exception.SelfException;
import com.wj.tool.common.utils.UuidUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Optional;

@Service
public class BookShelfServiceImpl implements BookShelfService {

    @Resource
    private BookRepository bookRepository;

    @Resource
    private BookMapper bookMapper;

    @Resource
    private BookShelfRepository bookShelfRepository;

    @Resource
    private BookShelfMapper bookShelfMapper;


    @Override
    public int isShelf(HttpServletRequest request, Long bookId) {
        Object idObj = request.getSession().getAttribute("id");
        if (idObj != null){
            Long id = Long.valueOf(idObj.toString());
            Optional<BookShelfEntity> optional = bookShelfRepository.findByUserIdAndBookId(id, bookId);
            if (optional.isPresent()){
                return 1;
            }
        }
        return 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateShelf(Long bookId, Long userId, int sort) {
        BookShelfEntity bookShelfEntity = null;
        Optional<BookShelfEntity> optional = bookShelfRepository.findByUserIdAndBookId(userId, bookId);
        if (optional.isPresent()){
            bookShelfEntity = optional.get();
            bookShelfEntity.setSort(sort);
        } else {
            bookShelfEntity = new BookShelfEntity();
            bookShelfEntity.setId(UuidUtils.createId());
            bookShelfEntity.setUserId(userId);
            bookShelfEntity.setBookId(bookId);
            bookShelfEntity.setSort(sort);
        }
        bookShelfRepository.save(bookShelfEntity);
        return 1;
    }

    @Override
    public List<BookShelfVO> queryByUserId(Long id) {
        List<BookShelfVO> list = bookShelfMapper.queryListByUserId(id);
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addShelf(HttpServletRequest request, Long bookId) {
        Object idObj = request.getSession().getAttribute("id");
        if (idObj != null){
            Long id = Long.valueOf(idObj.toString());
            Optional<BookShelfEntity> optional = bookShelfRepository.findByUserIdAndBookId(id, bookId);
            if (optional.isPresent()){
                throw new SelfException("书架上已存在");
            } else {
                BookShelfEntity entity = new BookShelfEntity();
                entity.setId(UuidUtils.createId());
                entity.setBookId(bookId);
                entity.setUserId(id);
                entity.setSort(1);
                bookShelfRepository.save(entity);
                return 1;
            }
        } else {
            throw new SelfException("请登录");
        }
    }

    @Override
    public int deleteShelf(Long bookId, Long userId) {
        Optional<BookShelfEntity> optional = bookShelfRepository.findByUserIdAndBookId(userId, bookId);
        if (optional.isPresent()){
            BookShelfEntity entity = optional.get();
            bookShelfRepository.deleteById(entity.getId());
            return 1;
        }
        return 0;
    }

    @Override
    public BookShelfVO findByBookId(HttpServletRequest request, Long bookId) {
        Object idObj = request.getSession().getAttribute("id");
        Long userId = 1l;
        if (idObj != null){
            userId = Long.valueOf(idObj.toString());
        }
        BookShelfVO mySelfChapterVO = bookShelfMapper.getMyShelfChapterInfo(userId, bookId);
        return mySelfChapterVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateShelfChapter(HttpServletRequest request, Long bookId, int sort) {
        Object idObj = request.getSession().getAttribute("id");
        Long userId = 1l;
        if (idObj != null){
            userId = Long.valueOf(idObj.toString());
        }
        BookShelfEntity entity = null;
        Optional<BookShelfEntity> optional = bookShelfRepository.findByUserIdAndBookId(userId, bookId);
        if (optional.isPresent()){
            entity = optional.get();
            entity.setSort(sort);
        } else {
            entity = new BookShelfEntity();
            entity.setId(UuidUtils.createId());
            entity.setBookId(bookId);
            entity.setUserId(userId);
            entity.setSort(sort);
            bookShelfRepository.save(entity);
        }
    }
}